package com.unionfetch.content_home.ui.auth

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.unionfetch.content_home.R
import com.unionftech.common.R.string
import com.unionfetch.content_home.bean.UserStatus
import com.unionftech.common.R.drawable
import com.unionftech.common.base.BaseActivity
import com.unionftech.common.base.IUpdate
import com.unionftech.common.common.GlobalInfoManager
import com.unionftech.common.common.bean.SystemUserInfo
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.ToastUtils

class ResultOfKYCAuthActivity : BaseActivity(), IUpdate {
    private val mBtnDepositNow by lazy { mRootView.findViewById<Button>(R.id.btn_deposit_now) }
    private val mTvResultSubTitle by lazy { mRootView.findViewById<TextView>(R.id.tv_result_sub_title) }
    private val mTvResultTitle by lazy { mRootView.findViewById<TextView>(R.id.tv_result_title) }

    companion object {
        @JvmStatic
        fun startThis(context: Context) {
            Intent(context, ResultOfKYCAuthActivity::class.java).apply {
                if (context !is Activity) {
                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                }
                context.startActivity(this)
            }
        }
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initLayout() = R.layout.activity_result_of_kycauth
    override fun initLanguageText() {
        super.initLanguageText()
        mTvResultTitle.text = DynamicResourceManager.getString(mContext, string.submit_success)
        mTvResultSubTitle.text =
            DynamicResourceManager.getString(mContext, string.please_wait_tips)
        mBtnDepositNow.text = DynamicResourceManager.getString(mContext, string.deposit_now)
    }

    override fun setListener() {
        super.setListener()
        mBtnDepositNow.setOnClickListener(this)
    }

    override fun setView() {
        super.setView()
        initTitleBar()
        loadData()
    }

    private fun initTitleBar() {
        hideCenterTitle()
        showBackImg(drawable.title_back)
    }

    override fun doClick(v: View?) {
        super.doClick(v)
        when (v?.id) {
            R.id.btn_deposit_now -> {
                //todo:立即入金
                ToastUtils.longToast(
                    DynamicResourceManager.getString(
                        mContext, string.developing
                    )
                )
            }
        }
    }

    override fun loadData() {
        super.loadData()
        getSystemUserInfo()
    }

    private fun getSystemUserInfo() {
        val systemUserInfo = GlobalInfoManager.getInstance().getSystemUserInfo()
        if (systemUserInfo != null) {
            updateResult(systemUserInfo)
        } else {
            GlobalInfoManager.getInstance().update(GlobalInfoManager.UPDATE_USR_INFO)
            GlobalInfoManager.getInstance().addListener(this)
        }
    }

    override fun onUpdate(obj: Any?) {
        if (obj is SystemUserInfo) {
            updateResult(obj)
        }
    }

    private fun updateResult(user: SystemUserInfo) {
        LogUtils.d("ResultOfKYCAuthActivity", "updateResult---->$user")
        if (user.status == UserStatus.USER_SUCCESS.status) {
            //更新申请成功，立即入金
            updateSuccess()
        } else {
            //审核中
            updateWaitingUI()
        }
    }

    private fun updateWaitingUI() {
        mBtnDepositNow.visibility = View.GONE
    }

    private fun updateSuccess() {
        mBtnDepositNow.visibility = View.VISIBLE
    }
}